﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace QLVeChuyenBay
{
    public partial class frmdanhsachCB : Form
    {
        public frmdanhsachCB()
        {
            InitializeComponent();
        }

        ChuyenBayBUS cbbus = new ChuyenBayBUS();

        private void frmdanhsachCB_Load(object sender, EventArgs e)
        {
            List<TuyenBayDTO> dsTuyenBay = new List<TuyenBayDTO>();
            TuyenBayBUS tbbus = new TuyenBayBUS();
            dsTuyenBay = tbbus.LayTuyenBay();
            cbTuyenBay.DataSource = dsTuyenBay;
            cbTuyenBay.DisplayMember = "MaTB";
            cbTuyenBay.ValueMember = "MaTB";
            cbTuyenBay.SelectedIndex = -1;

            int flag = 0;
            if (chkbHienThi.Checked == true)
                flag = 1;
            List<ChuyenBayDTO> listcbdto = new List<ChuyenBayDTO>();
            listcbdto = cbbus.TraCuuChuyenBay(flag);
            for (int i = 0; i < listcbdto.Count; i++)
            {
                //dgvSanBay.Rows.Add(i + 1, ds[i].MaSanBay, ds[i].TenSanBay);
               dgvDsCB.Rows.Add(i + 1, listcbdto[i].MaCB,listcbdto[i].SanbayDi,listcbdto[i].sanbayDen,listcbdto[i].NgayGio,listcbdto[i].ThoiGianBay,listcbdto[i].SoGheTrong,listcbdto[i].SoGheDat);
            }
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            dgvDsCB.Rows.Clear();
            int flag = 0;
            if (chkbHienThi.Checked == true)
                flag = 1;
            List<ChuyenBayDTO> listcbdto = new List<ChuyenBayDTO>();
            ChuyenBayDTO cbdto = new ChuyenBayDTO();
            cbdto.MaCB = txtMaCB.Text;

            if (cbTuyenBay.SelectedIndex != -1)
                cbdto.MaTB = cbTuyenBay.SelectedValue.ToString();

            //cbdto.NgayGio = dtNgayBay.Value;
            if(dtNgayBay.Value.ToString("MM/dd/yyyy")!="01/01/1753")
                cbdto.NgayGio = dtNgayBay.Value;
            //cbdto.ThoiGianBay = int.Parse(cbThoiGianBay.SelectedValue.ToString());
            //cbdto.SoGheTrong = int.Parse(cbSoGheTrong.SelectedValue.ToString());

            listcbdto = cbbus.TraCuuCBTheoYeuCau(cbdto,flag);
            for (int i = 0; i < listcbdto.Count; i++)
            {
                //dgvSanBay.Rows.Add(i + 1, ds[i].MaSanBay, ds[i].TenSanBay);
                dgvDsCB.Rows.Add(i + 1, listcbdto[i].MaCB, listcbdto[i].SanbayDi, listcbdto[i].sanbayDen, listcbdto[i].NgayGio, listcbdto[i].ThoiGianBay, listcbdto[i].SoGheTrong, listcbdto[i].SoGheDat);
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                this.Close();
            }
        }

        private void cbTuyenBay_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbTuyenBay.SelectedIndex != -1 && cbTuyenBay.Focused == true)
            {
                TuyenBayDTO tbdto = new TuyenBayDTO();
                TuyenBayBUS tbbus = new TuyenBayBUS();
                string matb = cbTuyenBay.SelectedValue.ToString();
                tbdto = tbbus.LayMotTuyenbay(matb);
                lbTuyenBay.Text = tbdto.SanBayDi + " - " + tbdto.SanBayDen;
            }
        }
    }
}
